import tui from '../../../common/httpRequest'
Page({
    data: {
        cartList: [],
        isAll: false,
        totalPrice: 0,
        buyNum: 0,
        cartIds: [], //购物车id
        actions: [{
            name: '删除',
            color: '#fff',
            fontsize: 28,
            width: 64,
            background: '#F82400'
        }],
        isEdit: false,
        // pageIndex: 1,
        loadding: false,
        // pullUpOn: true
    },
    calcHandle() {
        let buyNum = 0;
        let totalPrice = 0;
        let selectedNum = 0;
        this.data.cartList.map((item) => {
            if (item.selected) {
                buyNum += item.buyNum;
                totalPrice += item.price * item.buyNum;
                selectedNum++
            }
        })
        this.setData({
            isAll: selectedNum === this.data.cartList.length,
            buyNum: buyNum,
            totalPrice: totalPrice
        })
    },
    onLoad: function (options) {
        // this.calcHandle()
        var CartList = wx.getStorageSync('cartList')
        this.setData({
            cartList: CartList
        })
    },
    changeNum: function (e) {
        console.log(e.detail.value)
        console.log(`cartList[${e.detail.index}].buyNum`)

        this.setData({
            [`cartList[${e.detail.index}].buyNum`]: e.detail.value
        }, () => {
            this.calcHandle()
        })
    },
    //左滑删除
    leftdel: function (e) {
        let index = e.currentTarget.dataset.index;
        console.log(e)
        var cartList = this.data.cartList
        cartList.splice(index, 1); // 删除 splice(index下标，1删除的个数)
        this.setData({
            cartList: cartList
        })
        this.calcHandle()
        wx.setStorageSync('cartList', cartList)
    },
    // 购物车删除商品start
    btndel() {
        var list = this.data.cartList;
        var index = -1; //要开始删除的位置
        var delnum = 0; //要删除的数量
        for (var i = 0; i < list.length; i++) {
            //如果当前商品已选中
            if (list[i].selected) {
                if (index == -1) {
                    index = i; //得到要开始删除的位置（第一个勾选的商品的位置）
                }
                delnum++;
            }
        }
        list.splice(index, delnum); //删除
        this.setData({
            cartList: list
        })
        this.calcHandle()
        wx.setStorageSync('cartList', list)
    },
    // 购物车删除商品end
    editGoods: function () {
        this.setData({
            isEdit: !this.data.isEdit
        })

    },


    btnPay() {
        for (var i = 0; i < this.data.cartList.length; i++) {
            var carts = wx.getStorageSync('carts') || []
            if (this.data.cartList[i].selected === true) {
                wx.setStorageSync('carts', [this.data.cartList[i], ...carts])
                console.log(this.data.cartList[i])
            }
        }
        tui.href('../../../Packages/page1/pages/order/submitOrder/submitOrder')
    },

    buyChange(e) {
        this.setData({
            cartIds: e.detail.value
        }, () => {
            let data = this.data.cartList
            data.map(item => {
                //如果购物车id为数字统一转成字符串
                if (~this.data.cartIds.indexOf(item.id)) {
                    item.selected = true;
                } else {
                    item.selected = false;
                }
            })
            this.setData({
                cartList: data
            }, () => {
                this.calcHandle()
            })
        })
    },
    checkAll(e) {
        this.setData({
            isAll: !this.data.isAll
        }, () => {
            let buyNum = 0;
            let totalPrice = 0;
            let data = this.data.cartList
            data.map((item) => {
                item.selected = this.data.isAll;
                if (this.data.isAll) {
                    buyNum += item.buyNum;
                    totalPrice += item.price * item.buyNum;
                }
            })
            this.setData({
                cartList: data,
                totalPrice: totalPrice,
                buyNum: buyNum
            })
        })
    },
})